
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <memory>

#include "TcpSocket.hpp"
#include "Log.hpp"

int main(int argc, char *args[])
{
    if (argc != 3)
    {
        [args]
        { printf("%s 需要目的IP port\n", args[0]); }();
        exit(1);
    }
    struct sockaddr_in goal;
    std::unique_ptr<MySocket> ms(new MySocket);
    int cline_socket = ms->_socket_set();
    // 1.保留目标信息
    // 2.建立连接
    ms->Connect(cline_socket, atoi(args[2]), args[1]);
    while (1)
    {

        int a = 1;
        int b = 2;
        std::string op("+");
        std::cout << "请输入a# ";
        std::cin >> a;
        std::cout << "请输入b# ";
        std::cin >> b;
        std::cout << "请输入操作符# ";
        std::cin >> op;

        Ser_Change::DataConversion data(a, b, op);
        std::string t1 = data.serialize();
        Ser_Change::DataConversion data1;
        data1.Disserialize(t1);
        // 向网络套接字中写消息，我们也可以使用send借口，sendto是给udp协议使用的接口
        ms->Send(cline_socket, data.serialize());

        char buff[1024];
        if (!ms->Recv(cline_socket, buff, sizeof buff - 1))
        {
            Logmessage(FATIL, "cline recv fail");
        }
        std::string buff_str(buff);
        Ser_Change::Response tmp;
        int result = -1;
        tmp.Disserialize(buff_str, &result);
        std::cout << result << std::endl;
    }

    // 3.发送信息
    // 4.回收信息
    close(cline_socket);
    return 0;
}
